package com.cyj.controller;

import com.cyj.entity.AnimeRegion;
import com.cyj.entity.FileEntity;
import com.cyj.service.AnimeRegionService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

@Controller
@RequestMapping("/region")
public class AnimeRegionController {

    @Autowired
    private AnimeRegionService animeRegionService;

    @RequestMapping("/regions")
    public String regions(Locale locale){

        return "animeRegionList";
    }

    @ResponseBody
    @RequestMapping(value = "/animeRegionList",produces = "application/json;charset=utf-8")
    public Map animeRegion(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model) throws IOException {
        Map map = new HashMap<>();

        PageInfo page  = animeRegionService.selectAnimeRegionList(map,pn,5);

        map.put("pageInfo",page);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/allAnimeRegionList",produces = "application/json;charset=utf-8")
    public Map allanimeRegionList() throws IOException {
        Map<String,Object> map = new HashMap<String,Object>();

        List<AnimeRegion> list = animeRegionService.allAnimeRegion(map);
        if(list.size()>0){
            map.put("code",200);
            map.put("msg","发行地区查找成功");
            map.put("data",list);
        }else{
            map.put("code",100);
            map.put("msg","发行地区查找失败");
        }

        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/regionBy",produces = "application/json;charset=utf-8")
    public Map regionBy(HttpServletRequest request){
        Map<String,Object> map = new HashMap<String,Object>();
        Integer regionId = Integer.parseInt(request.getParameter("regionId"));

        map.put("regionId",regionId);

        AnimeRegion animeRegion = animeRegionService.selectAnimeRegion(map);

        if(animeRegion!=null){
            map.put("code", 200);
            map.put("msg", "发行地区查找成功");
            map.put("data",animeRegion);
        }else {
            map.put("code", 100);
            map.put("msg", "发行地区查找失败");
        }

        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/updateAnimeRegion",produces = "application/json;charset=utf-8")
    public Map updateAnimeRegion(HttpServletRequest request, FileEntity entity){

        Map<String, String[]> map = request.getParameterMap();
        AnimeRegion animeRegion = new AnimeRegion();

        try {
            BeanUtils.populate(animeRegion,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        System.out.println(animeRegion);

        int num = animeRegionService.updateAnimeRegion(animeRegion);

        Map<String,Object> map2 = new HashMap<String,Object>();

        if (num>0){
            map2.put("code", 200);
            map2.put("msg", "发行地区修改成功");
        }else {
            map2.put("code", 100);
            map2.put("msg", "发行地区修改失败");
        }

        return map2;
    }

    @ResponseBody
    @RequestMapping(value = "/addAnimeRegion",produces = "application/json;charset=utf-8")
    public Map addAnimeRegion(@RequestBody AnimeRegion animeRegion){

        Map<String,Object> map = new HashMap<String,Object>();

        System.out.println(animeRegion);

        int num = animeRegionService.addAnimeRegion(animeRegion);


        if(num>0){
            map.put("code",200);
            map.put("msg","添加发行地区成功！");
        }else{
            map.put("code",100);
            map.put("msg","添加发行地区失败！");
        }

        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/delAnimeRegion",produces = "application/json;charset=utf-8")
    public Map delAnimeRegion(@RequestBody AnimeRegion animeRegion){

        Map<String,Object> map = new HashMap<String,Object>();

        System.out.println(animeRegion);

        int num = animeRegionService.deleteAnimeRegion(animeRegion.getRegionId());

        if(num>0){
            map.put("code",200);
            map.put("msg","发行地区删除成功！");
        }else{
            map.put("code",100);
            map.put("msg","发行地区删除失败！");
        }


        return map;
    }

}
